package com.ple.app.sevlet;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cov_neeg on 2016/12/26.
 * 与服务器的交互再次模块
 * 只需要加入服务端连接地址即可
 */
public class GetFromServer {
    public static final String seed_url="http://192.168.13.117:8080/Information_server/Seed";
    public static final String manure_url="http://192.168.13.117:8080/Information_server/Manure";
    public static final String cultivate_url="http://192.168.13.117:8080/Information_server/Cultivate";
    public static final String dk_url="http://192.168.13.117:8080/Information_server/Dk";
    public static final String soil_url="http://192.168.13.117:8080/Information_server/Soildata";
    public static final String soilup_url="http://192.168.13.117:8080/Information_server/SoilpontUp";

   private String result="";
/**
 * 从服务端获得种子信息
 */
public String dopost(String jsonstring,String jsondelete,String url){
    HttpClient httpClient=new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),3000);
    HttpConnectionParams.setSoTimeout(httpClient.getParams(),3000);
    ConnManagerParams.setTimeout(httpClient.getParams(),3000);
    HttpPost hp=new HttpPost(url);
    NameValuePair param1=new BasicNameValuePair("ID",jsonstring);
    NameValuePair param2=new BasicNameValuePair("delete_id",jsondelete);
    List<NameValuePair> params=new ArrayList<NameValuePair>();
   params.add(param1);params.add(param2);
    HttpEntity he;
    try {
        he=new UrlEncodedFormEntity(params,"GBK");
        hp.setEntity(he);
        HttpResponse response=httpClient.execute(hp);//发起请求并得到回应
        if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
            HttpEntity het=response.getEntity();
            InputStream is= het.getContent();
            BufferedReader br=new BufferedReader(new InputStreamReader(is,"GBK"));
            String readline=null;
            while ((readline=br.readLine())!=null){
                result+=readline;
            }
            if(result.isEmpty()){result="1";}
            is.close();
        }//if
        //else{result=null;}
    }  catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}
    /**
     * 更新数据给服务端
     */
    public void postDB(String jsonstring,String url){
        HttpClient httpClient=new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),3000);
        HttpConnectionParams.setSoTimeout(httpClient.getParams(),3000);
        ConnManagerParams.setTimeout(httpClient.getParams(),3000);
        HttpPost hp=new HttpPost(url);
        NameValuePair param1=new BasicNameValuePair("data",jsonstring);
        List<NameValuePair> params=new ArrayList<NameValuePair>();
        params.add(param1);
        HttpEntity he;
        try {
            he=new UrlEncodedFormEntity(params,"GBK");
            hp.setEntity(he);
            HttpResponse response=httpClient.execute(hp);//得到服务器返回的数据
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
